using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WCF = System.Web.Configuration.WebConfigurationManager;
using System.Data.SqlClient;

/// <summary>
/// Summary description for NguoiDung_Data
/// </summary>
public class NguoiDung_Data
{
    /*
    public void insert(NguoiDung_Info ND)
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        //SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "sp_ThemNguoiDung";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50);
            Command.Parameters["@TenDangNhap"].Value = ND.TenDangNhap;
            Command.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50);
            Command.Parameters["@MatKhau"].Value = ND.MatKhau;
            Command.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
            Command.Parameters["@Email"].Value = ND.Email;
            Command.Parameters.Add("@HoTen", SqlDbType.NVarChar, 100);
            Command.Parameters["@HoTen"].Value = ND.HoTen;
            Command.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 250);
            Command.Parameters["@DiaChi"].Value = ND.DiaChi;
            Command.Parameters.Add("@DienThoai", SqlDbType.NVarChar, 50);
            Command.Parameters["@DienThoai"].Value = ND.DienThoai;
            Command.Parameters.Add("@Fax", SqlDbType.NVarChar, 50);
            Command.Parameters["@Fax"].Value = ND.Fax;
            Command.Parameters.Add("@MaPhanQuyen", SqlDbType.Int, 4);
            Command.Parameters["@MaPhanQuyen"].Value = ND.MaPhanQuyen;
            Command.Parameters.Add("@NgayKhoiTao", SqlDbType.DateTime, 8);
            Command.Parameters["@NgayKhoiTao"].Value = System.DateTime.Today;
            SqlDataReader reader;
            reader = Command.ExecuteReader();
            //adapter.InsertCommand = Command;
        }

        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }*/

    //Duy thuc hien
    
    public void insert(NguoiDung_Info ND)
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        //SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "insert_NguoiDung";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50);
            Command.Parameters["@TenDangNhap"].Value = ND.TenDangNhap;
            Command.Parameters.Add("@MatMa", SqlDbType.NVarChar, 50);
            Command.Parameters["@MatMa"].Value = ND.MatKhau;
            Command.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
            Command.Parameters["@Email"].Value = ND.Email;
            Command.Parameters.Add("@HoTen", SqlDbType.NVarChar, 100);
            Command.Parameters["@HoTen"].Value = ND.HoTen;
            Command.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 250);
            Command.Parameters["@DiaChi"].Value = ND.DiaChi;
            Command.Parameters.Add("@DienThoai", SqlDbType.NVarChar, 50);
            Command.Parameters["@DienThoai"].Value = ND.DienThoai;
            Command.Parameters.Add("@NgaySinh", SqlDbType.DateTime);
            Command.Parameters["@NgaySinh"].Value = ND.NgaySinh;
            Command.Parameters.Add("@CMND", SqlDbType.NVarChar, 10);
            Command.Parameters["@CMND"].Value = ND.CMND;
            Command.Parameters.Add("@Fax", SqlDbType.NVarChar, 50);
            Command.Parameters["@Fax"].Value = ND.Fax;
            Command.Parameters.Add("@MaThe", SqlDbType.VarBinary, 100);
            Command.Parameters["@MaThe"].Value = ND.MaThe;
            SqlDataReader reader;
            reader = Command.ExecuteReader();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    public void update(NguoiDung_Info ND)
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        //SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "sp_CapNhatNguoiDung";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50);
            Command.Parameters["@TenDangNhap"].Value = ND.TenDangNhap;
            Command.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50);
            Command.Parameters["@MatKhau"].Value = ND.MatKhau;
            Command.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
            Command.Parameters["@Email"].Value = ND.Email;
            Command.Parameters.Add("@HoTen", SqlDbType.NVarChar, 100);
            Command.Parameters["@HoTen"].Value = ND.HoTen;
            Command.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 250);
            Command.Parameters["@DiaChi"].Value = ND.DiaChi;
            Command.Parameters.Add("@DienThoai", SqlDbType.NVarChar, 50);
            Command.Parameters["@DienThoai"].Value = ND.DienThoai;
            Command.Parameters.Add("@Fax", SqlDbType.NVarChar, 50);
            Command.Parameters["@Fax"].Value = ND.Fax;
            Command.Parameters.Add("@MaPhanQuyen", SqlDbType.Int, 4);
            Command.Parameters["@MaPhanQuyen"].Value = ND.MaPhanQuyen;
            Command.Parameters.Add("@NgayKhoiTao", SqlDbType.DateTime, 8);
            Command.Parameters["@NgayKhoiTao"].Value = ND.NgayKhoiTao;
            Command.Parameters.Add("@NgayHieuChinh", SqlDbType.DateTime, 8);
            Command.Parameters["@NgayHieuChinh"].Value = System.DateTime.Today;
            Command.Parameters.Add("@SoLanDangNhap", SqlDbType.Int, 4);
            Command.Parameters["@SoLanDangNhap"].Value = ND.SoLanDangNhap;
            SqlDataReader reader;
            reader = Command.ExecuteReader();
            //adapter.InsertCommand = Command;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    public SqlDataAdapter LayDanhSachNguoiDung()
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "sp_DanhSachNguoiDung";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand = Command;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        return adapter;
    }

    public SqlDataAdapter LayThongTinNguoiDung(int Ma)
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "sp_LayThongTinNguoiDung";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@Ma", SqlDbType.Int, 4);
            Command.Parameters["@Ma"].Value = Ma;
            adapter.SelectCommand = Command;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        return adapter;
    }

    public bool KichHoatTaiKhoan(string MaKichHoat)
    {
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        //SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "KichHoatTaiKhoan";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@ChuoiDangKy", SqlDbType.NVarChar, 10);
            Command.Parameters["@ChuoiDangKy"].Value = MaKichHoat;
            SqlDataReader reader;
            reader = Command.ExecuteReader();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
            return false;
        }
        return true;
    }

    public int KiemTraNguoiDungHopLe(string TenDangNhap, string Email, string CMND)
    {
        int Kq;
        string connectionString = WCF.ConnectionStrings["gets_SPDG"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        if (connection.State == ConnectionState.Open)
            connection.Close();
        connection.Open();
        //SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            SqlCommand Command = new SqlCommand();
            Command.CommandText = "KiemTraNguoiDungHopLe";
            Command.Connection = connection;
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 20);
            Command.Parameters["@TenDangNhap"].Value = TenDangNhap;
            Command.Parameters.Add("@Email", SqlDbType.NVarChar, 100);
            Command.Parameters["@Email"].Value = Email;
            Command.Parameters.Add("@CMND", SqlDbType.NVarChar, 10);
            Command.Parameters["@CMND"].Value = CMND;
            Command.Parameters.Add("@KQ", SqlDbType.Int);
            Command.Parameters["@KQ"].Direction = ParameterDirection.Output;
            SqlDataReader reader;
            Command.ExecuteNonQuery();
            Kq = (int)Command.Parameters["@KQ"].Value;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        return Kq;
    }

	public NguoiDung_Data()
	{
		//
		// TODO: Add constructor logic here
		//
	}
}
